﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RaisingStudio.Data.Providers;
using System.Collections;

namespace RaisingStudio.Data.Linq
{
    public class ExpressionData<T, S> : RaisingStudio.Data.Expressions.QueryExpression<T>, IExpressionData<S>, IEnumerable<S>, IEnumerable
    {
        private DataContext dataContext;
        public DataContext DataContext
        {
            get { return dataContext; }
            set { dataContext = value; }
        }

        public ExpressionData()
        {

        }

        public ExpressionData(DataContext dataContext)
        {
            this.dataContext = dataContext;
        }

        public ExpressionData(DataContext dataContext, RaisingStudio.Data.Expressions.TableExpression table) : base(table)
        {
            this.dataContext = dataContext;
        }

        public ExpressionData(DataContext dataContext, RaisingStudio.Data.Expressions.ConditionExpression condition)
            : base(condition)
        {
            this.dataContext = dataContext;
        }

        public ExpressionData(DataContext dataContext, RaisingStudio.Data.Expressions.TableExpression table, RaisingStudio.Data.Expressions.ConditionExpression condition)
            : base(table, condition)
        {
            this.dataContext = dataContext;
        }

        public ExpressionData(DataContext dataContext, RaisingStudio.Data.Expressions.TableExpression table, RaisingStudio.Data.Expressions.ConditionExpression condition, RaisingStudio.Data.Expressions.ColumnExpressionCollection columns)
            : base(table, condition, columns)
        {
            this.dataContext = dataContext;
        }

        public ExpressionData(DataContext dataContext, T value) : base(value)
        {
            this.dataContext = dataContext;
        }

        public ExpressionData(DataContext dataContext, T value, RaisingStudio.Data.Expressions.TableExpression table)
            : base(table)
        {
            this.dataContext = dataContext;
            base.Value = value;
        }

        public ExpressionData(DataContext dataContext, T value, RaisingStudio.Data.Expressions.ConditionExpression condition)
            : base(condition)
        {
            this.dataContext = dataContext;
            base.Value = value;
        }

        public ExpressionData(DataContext dataContext, T value, RaisingStudio.Data.Expressions.TableExpression table, RaisingStudio.Data.Expressions.ConditionExpression condition)
            : base(table, condition)
        {
            this.dataContext = dataContext;
            base.Value = value;
        }

        public ExpressionData(DataContext dataContext, T value, RaisingStudio.Data.Expressions.TableExpression table, RaisingStudio.Data.Expressions.ConditionExpression condition, RaisingStudio.Data.Expressions.ColumnExpressionCollection columns)
            : base(table, condition, columns)
        {
            this.dataContext = dataContext;
            base.Value = value;
        }


        public ExpressionData(DataContext dataContext, RaisingStudio.Data.Expressions.IQueryExpression<T> queryExpression)
            : this(dataContext, queryExpression.Value, queryExpression.Table, queryExpression.Condition, queryExpression.Columns)
        {
        }

        public ExpressionData(DataContext dataContext, RaisingStudio.Data.Expressions.QueryExpression<T> queryExpression)
            : this(dataContext, queryExpression.Value, queryExpression.Table, queryExpression.Condition, queryExpression.Columns)
        {
        }

        #region IEnumerable 成员

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion

        #region IEnumerable<S> 成员

        public IEnumerator<S> GetEnumerator()
        {
            return this.dataContext.Query<S>(this).GetEnumerator();
        }

        #endregion
    }
}
